package com.qingsong.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qingsong.dto.PageDTO;
import com.qingsong.dto.RoomInsertDTO;
import com.qingsong.dto.RoomPageDTO;
import com.qingsong.dto.RoomUpdateDTO;
import com.qingsong.entity.Room;
import com.qingsong.exception.ServiceException;
import com.qingsong.mapper.DeptMapper;
import com.qingsong.mapper.RoomMapper;
import com.qingsong.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.sql.SQLRecoverableException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author qingsong
 */
@Service
@CacheConfig(cacheNames = "room")
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private DeptMapper deptMapper;

    @CacheEvict(allEntries = true)
    @Override
    public int insert(RoomInsertDTO roomInsertDTO) {
        Room room = BeanUtil.copyProperties(roomInsertDTO,Room.class);
        room.setId(null);
        room.setDeleted(0);
        room.setCreated(LocalDateTime.now());
        room.setUpdated(LocalDateTime.now());
        int result = roomMapper.insert(room);
        if(result <= 0){
            throw new ServiceException("数据库添加失败!");
        }
        return result;
    }

    @Cacheable(key = "#id", condition = "#id != null", unless = "#result == null")
    @Override
    public Room selectById(Long id) {
        Room result = roomMapper.selectById(id);
        if(ObjectUtil.isEmpty(result)){
            throw new ServiceException("ID不存在!");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int updateById(RoomUpdateDTO roomUpdateDTO) {
        //Hutool快速拷贝属性
        Room room = BeanUtil.copyProperties(roomUpdateDTO, Room.class);
        room.setCreated(null);
        room.setUpdated(LocalDateTime.now());
        int result = roomMapper.updateById(room);
        if(ObjectUtil.isEmpty(result)){
            throw new ServiceException("DataBase修改失败!");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteById(Long id) {
        //查询记录是否被引用
        if(deptMapper.existsRoomId(id)){
            throw new ServiceException("存在子记录,无法删除");
        }
        //DB逻辑删除
        int result = roomMapper.logicDeleteById(id);
        if(result <= 0){
            throw new ServiceException("数据库逻辑删除失败!");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteByIds(List<Long> ids) {
        //查询记录是否被引用
        if(deptMapper.existsRoomIds(ids)){
            throw new ServiceException("存在子记录,无法删除");
        }
        int result = roomMapper.logicDeleteByIds(ids);
        if(result <= 0){
            throw new ServiceException("数据库批量逻辑删除失败!");
        }
        return result;
    }

    @Cacheable(key = "#root.methodName", unless = "#result == null ")
    @Override
    public List<Room> list() {
        return roomMapper.list();
    }

    @Cacheable(key = "#root.methodName + ':' + " + "#pageDTO.getPageNum() + ':' + " + "#pageDTO.getPageSize()", unless = "#result == null ")
    @Override
    public PageInfo<Room> page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return new PageInfo<>(roomMapper.list());
    }

    @Cacheable(key = "#root.methodName + ':' + " +
            "#dto.getTitle() + ':' + " +
            "#dto.getPageNum() + ':' + " +
            "#dto.getPageSize()", unless = "#result == null ")
    @Override
    public PageInfo<Room> pageLikeTitle(RoomPageDTO dto) {
        //开启分页
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        //数据分页
        return new PageInfo<>(roomMapper.listLikeTitle(dto.getTitle()));
    }
}
